package com.lilicould.service.impl;

import com.lilicould.dao.EmployeeDao;
import com.lilicould.dao.HouseDao;
import com.lilicould.entity.Apartment;
import com.lilicould.entity.Building;
import com.lilicould.entity.Employee;
import com.lilicould.entity.House;
import com.lilicould.service.HouseService;
import com.lilicould.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;


public class HouseServiceImpl implements HouseService {

    public ArrayList<Apartment> selectAllApartments() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        HouseDao houseDao = sqlSession.getMapper(HouseDao.class);
        return houseDao.selectAllApartments();
    }

    @Override
    public ArrayList<House> selectAllHouses() {
        HouseDao houseDao = MyBatisUtil.getSqlSession().getMapper(HouseDao.class);
        EmployeeDao employeeDao = MyBatisUtil.getSqlSession().getMapper(EmployeeDao.class);
        ArrayList<House> houses = new ArrayList<>();
        ArrayList<Apartment> apartments = houseDao.selectAllApartments();
        for (Apartment apartment : apartments) {
            Building building = houseDao.selectBuildingByBuildingId(apartment.getBuildingId()); //获取对应楼栋信息
            Employee employee = employeeDao.selectEmployeeById(building.getManagerId()); //通过对应楼栋获取负责经理信息
            House house = new House(apartment.getBuildingId(), apartment.getApartmentNo(), apartment.getBedroomCount(), apartment.getBathroomCount(), building.getAddress(), building.getManagerId(), employee.getName(), employee.getPhone());
            houses.add(house);
        }
        return houses;
    }

    // 检验员工是否是该楼栋的经理或是否为空
    @Override
    public boolean isYours(String employeeId, String buildingId) {
        try {
            SqlSession sqlSession = MyBatisUtil.getSqlSession();
            HouseDao houseDao = sqlSession.getMapper(HouseDao.class);
            Building building = houseDao.selectBuildingByBuildingId(buildingId);
            return building == null || building.getManagerId().equals(employeeId);
        }catch (Exception e){
            System.out.println(e.getMessage());
            return false;
        }
    }

    // 插入新的房源信息,只需插入 Apartment表和Building表即可
    @Override
    public boolean insertHouse(House house) {
        try(SqlSession sqlSession = MyBatisUtil.getSqlSession()){
            HouseDao houseDao = sqlSession.getMapper(HouseDao.class);
            Building isExistBuilding = houseDao.selectBuildingByBuildingId(house.getBuildingId());
            if (isExistBuilding == null){
                int insertBuildingResult = houseDao.insertBuilding(house.getBuildingId(), house.getAddress(), house.getManagerId());
                int insertApartmentResult = houseDao.insertApartment(house.getBuildingId(), house.getApartmentNo(), house.getBedroomCount(), house.getBathroomCount());
                return insertBuildingResult > 0 && insertApartmentResult > 0;
            } else {
                int insertApartmentResult = houseDao.insertApartment(house.getBuildingId(), house.getApartmentNo(), house.getBedroomCount(), house.getBathroomCount());
                return insertApartmentResult > 0;
            }
        }catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
    }

    // 判断该公寓是否存在
    @Override
    public boolean isExist(String buildingId, String apartmentNo) {
        try {
            SqlSession sqlSession = MyBatisUtil.getSqlSession();
            HouseDao houseDao = sqlSession.getMapper(HouseDao.class);
            Apartment apartment = houseDao.selectApartmentByBuildingIdAndApartmentNo(buildingId, apartmentNo);
            return apartment != null;
        }catch (Exception e){
            System.out.println(e.getMessage());
            return false;
        }
    }
}
